#!/usr/bin/env python39
# -*- coding:utf-8 -*-
from requests_html import HTMLSession
from copyheaders import headers_raw_to_dict

str = b'''
# Cookie: https_waf_cookie=fa232764-b65d-4a131c9fa01f62ea072cdb4fe0bc9a29b8e8; Hm_lvt_f4fa5d56f272ae0a0777feec2184a97f=1650072557; BAIDU_SSP_lcr=https://hao.360.com/?src=lm&ls=n20bc1e3e9a; __gads=ID=830551d6ff8462c5-222fd728fad10091:T=1650072563:RT=1650072563:S=ALNI_MZ7etZYOSfZC8hMpY8GpVa5lUAsSw; Hm_lpvt_f4fa5d56f272ae0a0777feec2184a97f=1650072745; XSRF-TOKEN=eyJpdiI6IitJMnB5Q1ZlZHZ3TldOMjV1MUtCQnc9PSIsInZhbHVlIjoiZnl1WENwUmFLMHRXM3ppR1RJZXlzSWhTZms2dE5sK1NqNFVXSUxUdEtFQ2NkWjB0blJwQzA5Z081MU8wRVdoeSIsIm1hYyI6IjhkNzkyYjQxNTkzODIyNTIxMGE2ODQ5OGI4M2ZlMzk3MTYyYWNmZjEzYzc0NTYyMThhZmE4NGI0YjI4NTBkYzUifQ%3D%3D; laravel_session=EtiY1xw83fhTimrmeoAPRbsZvRrMdfU8hgP5X0ra
Host: www.douguo.com
Referer: https://www.douguo.com/
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.60 Safari/537.36
'''


class Menu:
    def __init__(self):
        self.url = 'https://www.douguo.com/caipu/%E5%AE%B6%E5%B8%B8%E8%8F%9C'
        self.base_url = 'https://www.douguo.com/caipu/%E5%AE%B6%E5%B8%B8%E8%8F%9C/0/{}'
        self.headers = headers_raw_to_dict(str)
        self.session = HTMLSession()

    def run(self, url):
        try:
            r = self.session.get(url=url, headers=self.headers)
            # print(r.status_code, r.encoding)
            demo_info = r.html.find('.cook-list li .cook-img')
            for detail in demo_info:
                cook_title = detail.attrs['title']
                # print(cook_title)
                cook_url = 'https://www.douguo.com' + detail.attrs['href']
                # print(cook_url)
                self.get_second_data(cook_url)

        except Exception as e:
            print('Error: %s' % e)

    def main(self):
        urls_list = [self.base_url.format(page) for page in range(0, 220, 20)]
        urls_list.insert(0, self.url)
        for url in urls_list:
            self.run(url)

    def get_second_data(self, cook_url):
        '''
        请求分页数据,获取详细内容
        :param cook_url:
        :return:
        '''

        self.headers['referer'] = cook_url
        try:
            rr = self.session.get(url=cook_url, headers=self.headers)
            # print(rr.status_code, rr.encoding)
            detail_src = rr.html.find('#banner a:first')
            for a in detail_src:
                print(a.attrs['href'])
            detail_src = rr.html.find('.rinfo.relative')
            for info in detail_src:
                print(info.find('h1')[0].text)
                print(info.find('.intro')[0].text)

        except Exception as e:
            print('Error:%s' % e)


if __name__ == '__main__':
    kk = Menu()
    kk.main()
